package com.yy.controller;

import com.yy.entity.ParkingLot;
import com.yy.entity.Vehicle;
import com.yy.service.ParkingLotService;
import com.yy.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Controller
@RequestMapping("/vehicle")
public class VehicleController {
    @Value("@{save.path}")
    String savePath;//图片存储路径
    private final VehicleService service;
    private final ParkingLotService parkingLotService;

    @Autowired
    public VehicleController(VehicleService service, ParkingLotService parkingLotService) {
        this.service = service;
        this.parkingLotService = parkingLotService;
    }
    @RequestMapping("/list")
    public String list(Model model) {
        List<Vehicle> list = service.getAll();
        model.addAttribute("list", list);
        return "/vehicle/list";//此处对应前端UI的URL
    }
    @RequestMapping(value = "/insert")
    public String insert(Model model) {
        model.addAttribute("vehicle", new Vehicle());
        List<ParkingLot> parkingLots = parkingLotService.getAll();
        model.addAttribute("parkingLots", parkingLots);
        return "/vehicle/edit";
    }
    @RequestMapping(value = "/edit/{id}")
    public String edit(@PathVariable("id")Long id, Model model){
        Vehicle entity=service.getById(id);
        model.addAttribute("vehicle",entity);
        List<ParkingLot> parkingLots=parkingLotService.getAll();
        model.addAttribute("parkingLots",parkingLots);
        return "/vehicle/edit";
    }
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@ModelAttribute("vehicle") Vehicle entity,
             @RequestParam("isActive") boolean isActive,
             @RequestParam("parkingLotId") Long parkingLotId,
             @RequestParam("file") MultipartFile file) throws Exception {

        entity.setActive(isActive);
        ParkingLot parkingLot = parkingLotService.getById(parkingLotId);
        entity.setParkingLot(parkingLot);

        //上传图片文件到指定目录
        String fileName = System.currentTimeMillis() + "-" + file.getOriginalFilename();//生成文件名
        Path saveTo = Paths.get(savePath, fileName);//设置文件存储的路径
        Files.write(saveTo, file.getBytes());//存储文件

        //存储图片URL
        entity.setPicUrl("/images/" + fileName);

        boolean result = entity.getId() != null ? service.update(entity) : service.insert(entity);

        return "redirect:/vehicle/list";
    }
    @RequestMapping(value = "/detail/{id}")
    public String detail(@PathVariable("id")Long id,Model model){
        Vehicle entity=service.getById(id);
        model.addAttribute("vehicle",entity);
        return "/vehicle/detail";
    }
    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable("id")Long id){
        boolean result=service.delete(id);
        return "redirect:/vehicle/list";
    }
}
